//
//  ZLExciseBeginCollectionViewCell.m
//  ZLExamination
//
//  Created by leilei on 2017/9/3.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExciseBeginCollectionViewCell.h"
#import "ZLExciseBeginQuestionTitleTableViewCell.h"
#import "ZLExciseBeginQuestionListTableViewCell.h"

static NSString *const ZLExciseBeginQuestionTitleTableViewCellID = @"ZLExciseBeginQuestionTitleTableViewCell";
static NSString *const ZLExciseBeginQuestionListTableViewCellID = @"ZLExciseBeginQuestionListTableViewCell";

@interface ZLExciseBeginCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, copy) NSArray<NSString *> *questionTitles;

@end

@implementation ZLExciseBeginCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _questionTitles = @[@"A",@"B",@"C",@"D"];
        
        [self.contentView addSubview:self.myTableView];
        
        [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
    }
    return self;
}

#pragma mark - tableview delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *answerStr1 = [_model.op_answer stringByReplacingOccurrencesOfString:@"A"withString:@""];
    NSString *answerSt2 = [answerStr1 stringByReplacingOccurrencesOfString:@"B"withString:@""];
    NSString *answerSt3 = [answerSt2 stringByReplacingOccurrencesOfString:@"C"withString:@""];
    NSString *answerSt4 = [answerSt3 stringByReplacingOccurrencesOfString:@"D"withString:@""];
    NSString *answerSt5 = [answerSt4 stringByReplacingOccurrencesOfString:@":"withString:@""];
    NSArray *titles = [answerSt5 componentsSeparatedByString:@";"];
    
    if (indexPath.section == 0) {
        ZLExciseBeginQuestionTitleTableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:ZLExciseBeginQuestionTitleTableViewCellID forIndexPath:indexPath];
        titleCell.questionLbl.text = _model.subject;
        return titleCell;

    } else {
        ZLExciseBeginQuestionListTableViewCell *listCell = [tableView dequeueReusableCellWithIdentifier:ZLExciseBeginQuestionListTableViewCellID forIndexPath:indexPath];
        if (_model.isSingles) {
            listCell.isSelected = [_model.questionAnswer isEqualToString:_questionTitles[indexPath.row]] ? YES : NO;
        } else {
            //多选
            if (_model.answers.count == 0) {
                listCell.multipleIsSelected = NO;
            } else {
                //从头看 是否包含这个数据, 如果有就是已经选中了
                if ([_model.answers containsObject:_questionTitles[indexPath.row]]) {
                    listCell.multipleIsSelected = YES;
                } else {
                    listCell.multipleIsSelected = NO;
                }
            }
        }
        listCell.optionsLbl.text = _questionTitles[indexPath.row];
        listCell.questionLbl.text = titles[indexPath.row];
        return listCell;
    }
}


-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0) {
        if (_answerCallBack) {
            _answerCallBack(_itemNumber, indexPath.row);
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 4;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

#pragma mark - lazy load
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.bounces = NO;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.estimatedRowHeight = 100;
        [_myTableView registerClass:[ZLExciseBeginQuestionTitleTableViewCell class] forCellReuseIdentifier:ZLExciseBeginQuestionTitleTableViewCellID];
        [_myTableView registerClass:[ZLExciseBeginQuestionListTableViewCell class] forCellReuseIdentifier:ZLExciseBeginQuestionListTableViewCellID];
        
    }
    return _myTableView;
}

@end
